<?php

/**

 * 系统设置
 * 
 * @access  public 
 * @author  qiaoshoujuan<qiao_sj@jsumt.com.cn> 
 */

namespace Home\Controller;

use Think\Controller;

class SystemController extends BaseController {

    public function _initialize() {
        parent::_initialize();

    }
    
    //组织机构信息
    public function organize(){
        if(IS_AJAX){
            $data = I('post.');
            $res = M('unit')->where('unit_no = '.$data['unit_no'])->save($data);
            $return['info'] = '修改成功'; 
            $this->ajaxReturn($return);
        }
        
        //单位信息
        $unit = M('unit')->find();
        
        //部门列表
        $department = M('department')->select();
        //获取顶级部门
        $this->topDept = M('department')->where('dept_parent = 0')->select();
        foreach($department as $k=>$v){
            $department[$k]['dept_user'] = M('user')->field('USER_ID,USER_NAME')->where('dept_id = '.$v['dept_id'])->select();
        }
        $this->unit = $unit;
        $this->department = $department;
        $this->display();
    }
    
    //新建部门
    public function saveDept(){
        $data = I('post.');
        if(!empty($data['manager'])){
            $data['manager'] = implode(',',$data['manager']).',';
        }
        if(!empty($data['assistant'])){
            $data['assistant_id'] = implode(',',$data['assistant']).',';
        }
        $res = M('department')->add($data);
        $new_id =  M()->getLastInsID();
        $new_insert = M('department')->where('dept_id = '.$new_id)->find();
        if($res){
            $return['status'] = 1;
            $return['new'] = $new_insert;
            $return['info'] = '操作成功';
        }else{
            $return['status'] = 0;
            $return['info'] = '操作失败';
        }
        $this->ajaxReturn($return);
    }
    
    //删除部门
    public function delDept(){
        $res = M('department')->where('dept_id = '.I('dept_id'))->delete();
        if($res !== false){
            $return['status'] = 1;
            $return['info'] = '刪除成功';
        }else{
            $return['status'] = 0;
            $return['info'] = '刪除失败';
        }
        $this->ajaxReturn($return);
    }
    
    //上传文件
    public function upload() {
        $this->upfile('./Public/Uploads/'.I('fileType').'/');
    }
    
}
